﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SimpleTaskManager.Domain.Entities;
using SimpleTaskManager.Domain.Abstract;

namespace SimpleTaskManager.WebUI.Controllers
{ 
    /// <summary>
    /// Controller for operations on skills
    /// </summary>
    public class SkillsController : Controller
    {
        private ISkillRepository repository;

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="skillRepository">repository of skills</param>
        public SkillsController(ISkillRepository skillRepository)
        {
            repository = skillRepository;
        }

        //
        // GET: /Skills/
        /// <summary>
        /// View with the full list of available skills and a form to create new skill
        /// </summary>
        /// <returns>Rendered view</returns>
        [Authorize]
        public ViewResult Index()
        {
            ViewBag.CanEdit = (User.IsInRole("Manager") || User.IsInRole("SuperUser"));
            return View(repository.Skills.ToList());
        }

        //
        // GET: /Skills/Create
        /// <summary>
        /// View for creating new skill
        /// </summary>
        /// <returns>View with the form for creating new skill</returns>
        [Authorize(Roles = "Manager, SuperUser")]
        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Skills/Create
        /// <summary>
        /// View which handles creation of new skill
        /// </summary>
        /// <param name="skill">new skill</param>
        /// <returns>Index view if operation was successful or error page if not</returns>
        [Authorize(Roles = "Manager, SuperUser")]
        [HttpPost]
        public ActionResult Create(Skill skill)
        {
            if (ModelState.IsValid)
            {
                repository.Add(skill);
                return RedirectToAction("Index");  
            }

            return View(skill);
        }
        
        //
        // GET: /Skills/Edit/5
        /// <summary>
        /// View with the form for editing chosen skill
        /// </summary>
        /// <param name="id">skill id</param>
        /// <returns>The rendered view</returns>
        [Authorize(Roles = "Manager, SuperUser")]
        public ActionResult Edit(int id)
        {
            Skill skill = repository.Skills.Single(s => s.SkillID == id);
            return View(skill);
        }

        //
        // POST: /Skills/Edit/5
        /// <summary>
        /// View for handling the edited skill
        /// </summary>
        /// <param name="skill">edited skill</param>
        /// <returns>Index view or error page if operation was unsuccessful</returns>
        [Authorize(Roles = "Manager, SuperUser")]
        [HttpPost]
        public ActionResult Edit(Skill skill)
        {
            if (ModelState.IsValid)
            {
                repository.Edit(skill);
                return RedirectToAction("Index");
            }
            return View(skill);
        }

        //
        // GET: /Skills/Delete/5
        /// <summary>
        /// View for deleting chosen skill
        /// </summary>
        /// <param name="id">id of the skill that should be deleted</param>
        /// <returns>Index view</returns>
        [Authorize(Roles = "Manager, SuperUser")]
        public ActionResult Delete(int id)
        {
            repository.Delete(id);
            return RedirectToAction("Index");
        }
    }
}